using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectQuangCao()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_QUANGCAO
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectQuangCaoPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_QUANGCAO,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListQuangCao", CreateViewModel(data));
		}

		public ActionResult SelectQuangCaoByIDVungQuangCao(int idvungquangcao, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDVungQuangCao = idvungquangcao,
				EnumViewModelType = EnumViewModel.ADMIN_QUANGCAO,
				CurrentPage = page
			};
			return View("SelectQuangCao", CreateViewModel(data));
		}

		public ActionResult SelectQuangCaoByIDVungQuangCaoPaging(int idvungquangcao, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDVungQuangCao = idvungquangcao,
				EnumViewModelType = EnumViewModel.ADMIN_QUANGCAO,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListQuangCao", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteQuangCao(int id)
		{
			var deleteItem = _repQuangCao.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
			var idvungquangcao = deleteItem.IDVungQuangCao;
			var result = _repQuangCao.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
                    FileHelper.DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repQuangCao.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfQuangCao(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_QUANGCAO,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateQuangCao(FormCollection forms)
		{
			string id = forms.GetValues("QuangCao_ID")[0];
			string idvungquangcao = forms.GetValues("QuangCao_IDVungQuangCao")[0];
			string url = forms.GetValues("QuangCao_URL")[0];
			var file = Request.Files["QuangCao_HinhMinhHoa"];
			string refName = "";
			var editItem = _repQuangCao.SelectByID(int.Parse(id));
			editItem.IDVungQuangCao = int.Parse(idvungquangcao.Replace(".", "").Replace(",", ""));
			editItem.URL = url;
			var result = false;
			if (file.ContentLength != 0)
			{
                result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), id, editItem.HinhMinhHoa, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
				editItem.HinhMinhHoa = refName;
			}
			result = _repQuangCao.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_QUANGCAO,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfQuangCao", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertQuangCao(FormCollection forms)
		{
			string idvungquangcao = forms.GetValues("QuangCao_IDVungQuangCao")[0];
			string url = forms.GetValues("QuangCao_URL")[0];
			var file = Request.Files["QuangCao_HinhMinhHoa"];
			string refName = "";
			var newItem = new QuangCao
			{
				IDVungQuangCao = int.Parse(idvungquangcao),
				URL = url,
				HinhMinhHoa = "noimages.jpg"
			};
			var result = _repQuangCao.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
                    result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), newItem.ID.ToString(), string.Empty, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
					if (result)
					{
						var addedItem = _repQuangCao.SelectByID(newItem.ID);
						addedItem.HinhMinhHoa = refName;
						_repQuangCao.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				IDVungQuangCao = int.Parse(idvungquangcao),
				EnumViewModelType = EnumViewModel.ADMIN_QUANGCAO,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectQuangCao", CreateViewModel(data));
		}


	}
}